package cc.mycraft;

import java.util.HashSet;
import java.util.Set;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Beacon;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;

import cc.mycraft.events.MyCraftResourceBeaconDropEvent;
import cc.mycraft.events.MyCraftResourceBeaconResetEvent;
import cc.mycraft.events.PlayerLogEvent;





public class MYCBeacon extends JavaPlugin implements Listener{
	
	private static Set<String> Got = new HashSet<String>();
	
	@Override
	public void onEnable() {
		Bukkit.getPluginManager().registerEvents(this, this);
		super.onEnable();
	}

	@Override
	public void onDisable() {
		HandlerList.unregisterAll();
		super.onDisable();
	}
	
	
	@EventHandler
	public void onTimer(MyCraftResourceBeaconResetEvent e) {
		for(String pN:Got) {
			if(Bukkit.getPlayer(pN)==null) {
				continue;
			}
			Bukkit.getPlayer(pN).sendMessage("§2你的资源信标已准备好资源，快去领取吧!");
		}
		Got.clear();
	}
	
	@EventHandler
	public void onPlayerClickBeacon(PlayerInteractEvent e) {
		boolean isCorrectAction = false;
		try {
			isCorrectAction = e.getPlayer().isSneaking()&&e.getClickedBlock().getType().equals(Material.BEACON)&&e.getAction().equals(Action.LEFT_CLICK_BLOCK);
		} catch (Exception e2) {
			isCorrectAction = false;
		}
		if(isCorrectAction) {
			Beacon b = (Beacon)e.getClickedBlock().getState();
			if(b.getCustomName()==null) {
				return;
			}
			String pN = e.getPlayer().getName();
			if(!b.getCustomName().equals("§r"+pN+"的资源信标")&&!b.getCustomName().equals(pN+"的资源信标")) {
				e.getPlayer().sendActionBar("§c你只能收获自己信标的资源");
				return;
			}
			if(Got.contains(pN)) {
				e.getPlayer().sendActionBar("§c资源信标还没准备好");
				return;
			}
			Block DropL = b.getWorld().getBlockAt(b.getX(),b.getY()+1,b.getZ());
			if(!DropL.isEmpty()) {
				e.getPlayer().sendActionBar("§c资源信标上方不为空，无法产生资源");
				return;
			}
			if(b.getTier()!=0) {
				e.getPlayer().sendActionBar("§c已激活的信标无法产生资源");
				return;
			}
			Block MaterialBlock = b.getWorld().getBlockAt(b.getX(),b.getY()-1,b.getZ());
			DropList dl; 
			switch (MaterialBlock.getType()) {
			case GOLD_BLOCK: dl = new DropList(Material.GOLD_INGOT,1); break;
			case EMERALD_BLOCK: dl = new DropList(Material.EMERALD,1); break;
			case DIAMOND_BLOCK: dl = new DropList(Material.DIAMOND,2); break;
			case QUARTZ_BLOCK: dl = new DropList(Material.QUARTZ,1); break;
			case LAPIS_BLOCK: dl = new DropList(Material.LAPIS_LAZULI,2); break;
			case REDSTONE_BLOCK: dl = new DropList(Material.REDSTONE,1); break;
			case GLOWSTONE: dl = new DropList(Material.GLOWSTONE_DUST,1); break;
			case COAL_BLOCK: dl = new DropList(Material.COAL,1); break;
			default: dl = new DropList(Material.IRON_INGOT,1); break;
			}
			int value = 0;
			for(Entity E:b.getLocation().getNearbyEntities(24, 24, 24)) {
				switch (E.getType()) {
				case VILLAGER: value++;break;
				case PIG: dl.addMaterial(Material.PORKCHOP);break;
				case COW: dl.addMaterial(Material.BEEF);break;
				case MUSHROOM_COW: dl.addMaterial(Material.BEEF);break;
				case RABBIT: dl.addMaterial(Material.RABBIT);break;
				case CHICKEN: 
					dl.addMaterial(Material.CHICKEN);
					dl.addMaterial(Material.FEATHER);
					break;
				case SHEEP: 
					dl.addMaterial(Material.MUTTON);
					dl.addMaterial(Material.WHITE_WOOL);
					break;
				case HORSE: dl.addMaterial(Material.LEATHER);break;
				default:
					break;
				}
			}
			if(value>0) {
				if(value>30) {
					value=30;
				}
			}else {
				e.getPlayer().sendActionBar("§c信标附近没有村民，无法产生资源");
				return;
			}
			dl.setAmount(value);
			Got.add(pN);
			Bukkit.getPluginManager().callEvent(new MyCraftResourceBeaconDropEvent(MYCPlugin.RESOURCEBEACON,e.getPlayer(),b,dl.getDrops(),dl.getDropAmount()));
		}
	}
	
	
	
	@EventHandler
	public void onPlayerBreakBeacon(BlockBreakEvent e) {
		if(e.isCancelled()) {
			return;
		}
		if(!e.getBlock().getType().equals(Material.BEACON)) {
			return;
		}
		
		if(e.getPlayer().getGameMode().equals(GameMode.CREATIVE)&&e.getPlayer().hasPermission("mycraft.cmd.admin")) {
			return;
		}
		
		Beacon b = (Beacon)e.getBlock().getState();
		if(b.getCustomName()==null) {
			Bukkit.getPluginManager().callEvent(new PlayerLogEvent(LogType.PLOG, e.getPlayer().getName(), e.getPlayer().getLocation(), Actions.PLAYER_RCB_GET, e.getBlock().getType().toString(), e.getBlock().getLocation(), "NONE", true));
			return;
		}
		e.setCancelled(true);
		String pN = e.getPlayer().getName();
		if(!b.getCustomName().equals("§r"+pN+"的资源信标")&&!b.getCustomName().equals(pN+"的资源信标")) {
			e.getPlayer().sendActionBar("§c你无法破坏别人的资源信标");
			return;
			/*
			if(b.getCustomName().length()<5) {
				e.getBlock().setType(Material.AIR);
				b.getWorld().dropItem(b.getLocation(), new ItemStack(Material.BEACON));
				Bukkit.getPluginManager().callEvent(new PlayerLogEvent(true, pN, "破坏了"+getLocStr(b)+"处命名错误资源信标"));
				return;
			} else {
				e.getPlayer().sendActionBar("§c你无法破坏别人的资源信标");
				return;
			}
			*/
		}
		e.getBlock().setType(Material.AIR);
		if(e.getPlayer().getGameMode().equals(GameMode.CREATIVE)) {
			return;
		}
		b.getWorld().dropItem(b.getLocation(), BeaconItem(pN));
		Bukkit.getPluginManager().callEvent(new PlayerLogEvent(LogType.PLOG, e.getPlayer().getName(), e.getPlayer().getLocation(), Actions.PLAYER_RCB_BREAK, e.getBlock().getType().toString(), e.getBlock().getLocation(), "NONE", true));
	}	
	
	private static ItemStack BeaconItem(String PlayerName) {
		ItemStack i = new ItemStack(Material.BEACON);
		ItemMeta im = i.getItemMeta();
		im.setDisplayName("§r"+PlayerName+"的资源信标");
		i.setItemMeta(im);
		return i;
	}
	
	private static String getLocStr(Beacon b) {
		return b.getWorld().getName()+","+b.getX()+","+b.getY()+","+b.getZ();
	}
	
}

